﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmDinhKhoanPhieuThuTien : Form
    {
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();

        public long ltien = 0;
        public long tongtien = 0;

        private PhieuThuTien phieuthutien;
        public PhieuThuTien PhieuThuTien
        {
            set { this.phieuthutien = value; }
            get { return this.phieuthutien; }
        }

        public FrmDinhKhoanPhieuThuTien(long tien)
        {
            InitializeComponent();
            ltien = tien;
        }

        private void FrmDinhKhoanPhieuThuTien_Load(object sender, EventArgs e)
        {
            txtSoChungTu.Text = this.phieuthutien.MaSo;
            txtMSKH.Text = this.phieuthutien.DoiTuong;
            List<ChiNhanhCongTy> lstChiNhanhCT = (from c in GetAllChiNhanhCongTy()
                                                  where c.Id == this.phieuthutien.IdChiNhanhCongTy
                                                  select c).ToList();
            txtChiNhanh.Text = lstChiNhanhCT[0].TenChiNhanh;

            dgvDinhKhoanPhieuThuTien.Columns["colSoTien"].ReadOnly = false;

            cboSoTaiKhoan.DataSource = GetAllTaiKhoanKeToan();
            cboSoTaiKhoan.DisplayMember = "MaSoTK";
            cboSoTaiKhoan.ValueMember = "Id";

            AddToGrid();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDinhKhoanPhieuThuTien_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tkkt in GetAllTaiKhoanKeToan()
                                                          where tkkt.Id == Convert.ToInt32(dgvDinhKhoanPhieuThuTien.Rows[e.RowIndex].Cells[0].Value.ToString())
                                                          select tkkt).ToList();
                string tenTK = lstTaiKhoanKeToan[0].TenTK;
                dgvDinhKhoanPhieuThuTien.Rows[e.RowIndex].Cells[2].Value = tenTK;
            }
            if (e.ColumnIndex == 3 && e.RowIndex > -1 && dgvDinhKhoanPhieuThuTien.Rows[e.RowIndex].Cells[0].FormattedValue.ToString() != "")
            {
                if (dgvDinhKhoanPhieuThuTien.Rows[e.RowIndex].Cells[3].Value == null)
                    dgvDinhKhoanPhieuThuTien.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            long tongBaBaMot = 0;
            for (int j = 1; j < dgvDinhKhoanPhieuThuTien.Rows.Count - 1; j++)
            {
                if (dgvDinhKhoanPhieuThuTien.Rows[j].Cells[3].Value != null)
                {
                    tongBaBaMot += Convert.ToInt64(dgvDinhKhoanPhieuThuTien.Rows[j].Cells[3].Value.ToString());
                }
            }
            tongtien = tongBaBaMot;
            dgvDinhKhoanPhieuThuTien.Rows[0].Cells[3].Value = tongBaBaMot;
           
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvDinhKhoanPhieuThuTien.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoanPhieuThuTien.Rows[i].Cells[0].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            if (dgvDinhKhoanPhieuThuTien.Rows[0].Cells[1].FormattedValue.ToString() == "Có")
            {
                MessageBox.Show("Định khoản " + dgvDinhKhoanPhieuThuTien.Rows[0].Cells[1].FormattedValue.ToString() + " phải chọn \"Nợ\" !!!", "Thông báo");
                return;
            }

            for (int i = 2; i < dgvDinhKhoanPhieuThuTien.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoanPhieuThuTien.Rows[i].Cells[1].FormattedValue.ToString() == "Nợ" || dgvDinhKhoanPhieuThuTien.Rows[i].Cells[1].FormattedValue.ToString() == "")
                {
                    MessageBox.Show("Các định khoản khác " + dgvDinhKhoanPhieuThuTien.Rows[0].Cells[0].FormattedValue.ToString() + " phải chọn \"Có\" !!!", "Thông báo");
                    return;
                }
            }

            if (ltien == tongtien)
            {
                EditOrRemoveDinhKhoan(phieuthutien.MaSo);

                ResultObj result = new ResultObj();
                
                for (int i = 1; i < dgvDinhKhoanPhieuThuTien.Rows.Count - 1; i++)
                {
                    if (dgvDinhKhoanPhieuThuTien.Rows[i].Cells[3].FormattedValue.ToString() != "0")
                    {
                        ButToan buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgay.Value;
                        buttoan.ChiNhanh = txtChiNhanh.Text;
                        buttoan.MaSoKhachHang = txtMSKH.Text;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = txtSoChungTu.Text;
                        buttoan.TKGoc = dgvDinhKhoanPhieuThuTien.Rows[0].Cells[0].FormattedValue.ToString();
                        buttoan.TKDoiUng = dgvDinhKhoanPhieuThuTien.Rows[i].Cells[0].FormattedValue.ToString();
                        buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoanPhieuThuTien.Rows[i].Cells[3].Value.ToString());
                        buttoan.GhiCo = 0;
                        result = buttoanMgt.SaveNewButToan(buttoan);

                        buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgay.Value;
                        buttoan.ChiNhanh = txtChiNhanh.Text;
                        buttoan.MaSoKhachHang = txtMSKH.Text;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = txtSoChungTu.Text;
                        buttoan.TKGoc = dgvDinhKhoanPhieuThuTien.Rows[i].Cells[0].FormattedValue.ToString();
                        buttoan.TKDoiUng = dgvDinhKhoanPhieuThuTien.Rows[0].Cells[0].FormattedValue.ToString();
                        buttoan.GhiNo = 0;
                        buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoanPhieuThuTien.Rows[i].Cells[3].FormattedValue.ToString());
                        result = buttoanMgt.SaveNewButToan(buttoan);
                    }
                }
                if (result.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");
                    this.Close();
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                else
                    MessageBox.Show("Thất bại");
            }
            else
            {
                MessageBox.Show("Số tiền định khoản không đúng", "Thông báo");
            }
        }

        public void AddToGrid()
        {
            DataGridViewRow dataRow = new DataGridViewRow();
            DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tk in GetAllTaiKhoanKeToan()
                                                      where tk.MaSoTK.StartsWith("111")
                                                      select tk).ToList();

            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";
            dataRow.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Nợ";
            dataRow.Cells.Add(cboCell);

            DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
            cell.Value = "";
            dataRow.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            dataRow.Cells.Add(cell);

            dgvDinhKhoanPhieuThuTien.Rows.Add(dataRow);
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj result = new ResultObj();
            result = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstChiNhanhCongTy = (List<ChiNhanhCongTy>)result.Result;
            return lstChiNhanhCongTy;
 
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj result = new ResultObj();
            result = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)result.Result;
            return lstTaiKhoanKeToan;
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoanPhieuThuTien.CurrentRow != null)
            {
                if (dgvDinhKhoanPhieuThuTien.CurrentRow.Index != (dgvDinhKhoanPhieuThuTien.Rows.Count - 1))
                {
                    dgvDinhKhoanPhieuThuTien.Rows.RemoveAt(dgvDinhKhoanPhieuThuTien.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoanPhieuThuTien.Rows[dgvDinhKhoanPhieuThuTien.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoanPhieuThuTien.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    this.tongtien = result;
                    dgvDinhKhoanPhieuThuTien.Rows[0].Cells[3].Value = result;
                }
            }
        }

        private void dgvDinhKhoanPhieuThuTien_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoanPhieuThuTien.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoanPhieuThuTien.ClearSelection();
                    dgvDinhKhoanPhieuThuTien.CurrentCell = dgvDinhKhoanPhieuThuTien.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoanPhieuThuTien.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }
    }
}
